package com.financial.business.yspay.service.query.impl;

import com.financial.business.yspay.connector.YsQueryFeign;
import com.financial.business.yspay.constants.FinancialQueryYsEnumConstants;
import com.financial.business.yspay.service.query.FinancialQuerySplitService;
import com.financial.business.yspay.utils.JsonUtils;
import com.financial.business.yspay.utils.apiresult.ApiResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author WANGNING
 * @Email wyn20140226@aliyun.com
 * @Description //银盛分账查询接口
 * @Date 2019/11/22 17:12
 */
@Service
public class FinancialQuerySplitServiceImpl implements FinancialQuerySplitService {
    public static final Logger logger = LoggerFactory.getLogger(FinancialQuerySplitServiceImpl.class);

    @Autowired
    private YsQueryFeign ysQueryFeign;

    @Override
    public ApiResult splitQuery(String requestParam) {
        logger.info("FinancialQuerySplitServiceImpl.splitQuery()----银盛分账查询获取到的json数据为：" + requestParam);
        Map<String, Object> mapParams = JsonUtils.jsonToMap(requestParam);
        Object tempParams = mapParams.get("out_trade_no");
        /*银盛交易号*/
        String outTradeNo = tempParams == null ? null : tempParams.toString();
        /*银盛平台编号---优店---gyyoudian*/
        tempParams = mapParams.get("src_usercode");
        String srcUsercode = tempParams == null ? null : tempParams.toString();
        if (StringUtils.isBlank(outTradeNo) || StringUtils.isBlank(srcUsercode)) {
            /*对所传字段进行非空校验判断*/
            logger.info("FinancialQuerySplitServiceImpl.splitQuery()------对所传字段有空值：" + requestParam);
            return ApiResult.failure(requestParam);
        }
        ApiResult ysQueryJson = null;
        try {
            /*组装银盛分账查询*/
            Map<String, Object> map = new HashMap<>();
            map.put("out_trade_no", outTradeNo);
            map.put("src_usercode", srcUsercode);
            String splitQueryJson = JsonUtils.toJSon(map);
            logger.info("FinancialQuerySplitServiceImpl.splitQuery()----拼装请求银盛分账查询接口前参数json数据为:" + splitQueryJson);
            ysQueryJson = ysQueryFeign.split(splitQueryJson);
            logger.info("FinancialQuerySplitServiceImpl.splitQuery()----拼装请求银盛分账查询接口返回参数json数据为:" + ysQueryJson);
            if (ysQueryJson.getCode() == FinancialQueryYsEnumConstants.ENUM_APIRESULT_SUCCESS && ysQueryJson.getResult() != null) {
                return ApiResult.success(ysQueryJson);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("FinancialQuerySplitServiceImpl.splitQuery()----银盛分账查询异常" + requestParam);
        }
        return ApiResult.failure(ysQueryJson);
    }
}
